package com.ef.domain;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.ef.constant.ELeadsClassStatus;

/**
 * leads的课程记录.<br/>
 * 在booking 的时候新建 LeadsClassEntity.<br/>
 * 当booking 的时候为 wait 状态时 waiting 的值为 true； 当checkin 的时候 设置 absense 的值。 当cancel
 * 的时候 设置 cancel 的值。
 * 
 * @author CYG create:2009-12-12 下午06:39:38
 */
@Entity
public class LeadsClassEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@ManyToOne(fetch = FetchType.EAGER)
	private Leads leads;

	@ManyToOne(fetch = FetchType.EAGER)
	private ClassEntity classEntity;

	@Temporal(TemporalType.TIMESTAMP)
	private Date bookingTime;

	@ManyToOne
	private User bookingUser;

	@Temporal(TemporalType.TIMESTAMP)
	private Date checkinTime;

	@ManyToOne
	private User checkinUser;

	@Temporal(TemporalType.TIMESTAMP)
	private Date cancelTime;

	@ManyToOne
	private User cancelUser;

	@Enumerated(value = EnumType.STRING)
	private ELeadsClassStatus classStatus;
	
    private Boolean bookingFromWaiting;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date oldStateTime;
    
    @ManyToOne
	private User oldStateUser;

	public Date getOldStateTime() {
		return oldStateTime;
	}

	public void setOldStateTime(Date oldStateTime) {
		this.oldStateTime = oldStateTime;
	}

	public User getOldStateUser() {
		return oldStateUser;
	}

	public void setOldStateUser(User oldStateUser) {
		this.oldStateUser = oldStateUser;
	}

	public Boolean getBookingFromWaiting() {
		return bookingFromWaiting;
	}

	public void setBookingFromWaiting(Boolean bookingFromWaiting) {
		this.bookingFromWaiting = bookingFromWaiting;
	}

	/**
	 * 在课程中的顺序
	 */
	private Integer classOrder;

	@Column(length = 200)
	private String comments;

	public boolean isBooking() {
		if (classStatus == null) {
			return false;
		}
		return classStatus.equals(ELeadsClassStatus.BOOKING);
	}

	public boolean isWaiting() {
		if (classStatus == null) {
			return false;
		}
		return classStatus.equals(ELeadsClassStatus.WAITING);
	}

	public boolean isCheckin() {
		if (classStatus == null) {
			return false;
		}
		return classStatus.equals(ELeadsClassStatus.CHECKIN);
	}

	public boolean isAbsence() {
		if (classStatus == null) {
			return false;
		}
		return classStatus.equals(ELeadsClassStatus.ABSENCE);
	}

	/**
	 * 是否已完成.
	 * 
	 * @return
	 */
	public boolean isFinish() {
		return isCheckin() || isAbsence()||isBooking();
	}

	public boolean isHaving() {
		return isCheckin() || isAbsence()||isBooking()||isWaiting();
	}

	
	public boolean isCancel() {
		if (classStatus == null) {
			return false;
		}
		return classStatus.equals(ELeadsClassStatus.CANCEL);
	}

	public boolean isClear() {
		if (classStatus == null) {
			return false;
		}
		return classStatus.equals(ELeadsClassStatus.CLEAR);
	}

	public Date getCheckinTime() {
		return checkinTime;
	}

	public void setCheckinTime(Date checkinTime) {
		this.checkinTime = checkinTime;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Leads getLeads() {
		return leads;
	}

	public void setLeads(Leads leads) {
		this.leads = leads;
	}

	public ClassEntity getClassEntity() {
		if (classEntity == null) {
			return new ClassEntity();
		}
		return classEntity;
	}

	public void setClassEntity(ClassEntity classEntity) {
		this.classEntity = classEntity;
	}

	public Date getBookingTime() {
		return bookingTime;
	}

	public void setBookingTime(Date bookingTime) {
		this.bookingTime = bookingTime;
	}

	public Date getCancelTime() {
		return cancelTime;
	}

	public void setCancelTime(Date cancelTime) {
		this.cancelTime = cancelTime;
	}

	public String getStatus() {
		if (this.isAbsence()) {
			return "Absence";
		}
		if (this.isCheckin()) {
			return "Checkin";
		}
		if (this.isCancel()) {
			return "Cancel";
		}
		if (this.isWaiting()) {
			return "Waiting";
		}
		if (this.isBooking()) {
			return "Booking";
		}
		if (this.isClear()) {
			return "Clear";
		}
		return "unknow";
	}

	public String getResponseString() {
		StringBuffer rsp = new StringBuffer();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sname = "";
		String eliteCard = "";
		String tel = "";
		if (this.getLeads() != null) {
			sname = this.getLeads().getName();
			eliteCard = this.getLeads().getEliteCard();
			tel = this.getLeads().getTelephone() + " "
					+ this.getLeads().getMobileTelephone();
		}
		String btime = "";
		if (this.getBookingTime() != null) {
			btime = sf.format(getBookingTime());
		}
		String ctime = "";
		if (this.getCancelTime() != null) {
			ctime = sf.format(getCancelTime());
		}
		rsp.append("<entity>").append("{id:").append(id).append(",sname:\"")
				.append(sname).append("\",card:\"").append(eliteCard).append(
						"\",tel:\"").append(tel).append("\",btime:\"").append(
						btime).append("\",ctime:\"").append(ctime).append(
						"\",status:\"").append(getStatus()).append(
						"\"}</entity>");

		return rsp.toString();
	}

	public User getBookingUser() {
		return bookingUser;
	}

	public void setBookingUser(User bookingUser) {
		this.bookingUser = bookingUser;
	}

	public User getCheckinUser() {
		return checkinUser;
	}

	public void setCheckinUser(User checkinUser) {
		this.checkinUser = checkinUser;
	}

	public User getCancelUser() {
		return cancelUser;
	}

	public void setCancelUser(User cancelUser) {
		this.cancelUser = cancelUser;
	}

	public ELeadsClassStatus getClassStatus() {
		return classStatus;
	}

	public void setClassStatus(ELeadsClassStatus classStatus) {
		this.classStatus = classStatus;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public Integer getClassOrder() {
		return classOrder;
	}

	public void setClassOrder(Integer classOrder) {
		this.classOrder = classOrder;
	}
}
